package ws.very.util.akka.cluster

import akka.cluster.ClusterEvent.UnreachableMember
import akka.cluster.ClusterEvent.MemberEvent
import akka.cluster.ClusterEvent.ClusterDomainEvent
import scala.concurrent.duration._
import akka.cluster.Member
import scala.concurrent.ExecutionContext
import akka.cluster.ClusterEvent.ClusterMetricsChanged
import akka.cluster.ClusterEvent.MemberRemoved
import akka.cluster.MemberStatus
case class Rejoin(m: Member)
trait UnreachableAutoJoinHandler extends ClusterListener {
  implicit val e: ExecutionContext = context.dispatcher
  protected val listen = Seq(classOf[ClusterDomainEvent])
  protected var timers = Map()
  def receive = {
    case Rejoin(m) =>
      cluster.join(m.address)
    case MemberRemoved(m, MemberStatus.Down) =>
      cluster.down(m.address)
      context.system.scheduler.schedule(0 second, 5 second, self, Rejoin(m))
    case other: MemberEvent =>
      println(other)
    case ClusterMetricsChanged(c) =>
    case wtf                      => println("wtf:" + wtf)
  }
}